<%=javascript_include_tag "work_time", :plugin=>'redmine_work_time' %>

<%= @message %>
<div style="text-align:right;">
<%= render :partial=>'select_project' %> <br/>
[<%= link_to(l(:wt_each_member_report), @link_params.merge(:action=>"show")) %>]
[<%= link_to(l(:wt_edit_relay), @link_params.merge(:action=>"edit_relay")) %>]
[<%= link_to(l(:wt_relay_total), @link_params.merge(:action=>"relay_total")) %>]
</div>

<h2><%=l(:wt_raw_total)%></h2>
<div style="text-align:right;">
[<%= link_to(l(:wt_data_download), @link_params.merge(:action=>"total_data")) %>]
</div>

<%
if !User.current.allowed_to?(:view_work_time_other_member, @project) then
  return
end
############################################## 集計表 #####################
%>
<table border="1">
<tr valign="top">
<td>
<div style="text-align:center;position:relative;top:1px;left:1px;font-size:2.0em;">
<%=link_to("<<", @link_params.merge(:day=>@last_month.day, :month=>@last_month.month, :year=>@last_month.year))%>
<%=@this_year %>-<%= @month_names[@this_month-1] %>
<%=link_to(">>", @link_params.merge(:day=>@next_month.day, :month=>@next_month.month, :year=>@next_month.year))%>
</div>
</td>
<td style="background:#ddd;"></td>
<%
max = @members.size
@members.each do |mem_info|
  pos=mem_info[0]
  user=mem_info[1]
%>
<td align="center">
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
<a href="JavaScript:member_pos('<%=url_for(@link_params)%>', <%=user.id%>,<%=pos%>,<%=max%>);"><%=pos%></a>
<%end%>
<div style="top:2px;width:56px;font-size:10px;">
<%= link_to(user, @link_params.merge(:action=>"show", :user=>user.id))%>
</div>
</td>
<%
end
%>
</tr>

<tr>
<td style="background:#ddd;">
</td>
<td style="background:#ddd;">
<%=@total_cost%>
</td>
<%
#---------------------------------------各人の合計を表示
@members.each do |mem_info|
  user=mem_info[1]
%>
<td style="background:#ddd;">
<%= (@member_cost.key?(user.id) && @member_cost[user.id] !=0 ) ? @member_cost[user.id] : "&nbsp;"%>
</td>
<%
end
%>
</tr>

<%
prjs = WtProjectOrders.find(:all, :order=>"dsp_pos", :conditions=>"uid=-1")

tickets = WtTicketRelay.find(:all, :order=>"position")
tic_max = tickets.size

#-------------------------------------- チケット行のループ
prjs.each do |po|
 dsp_prj = po.dsp_prj
 dsp_pos = po.dsp_pos
 next unless @prj_cost.key?(dsp_prj) # 値の無いプロジェクトはパス
 next unless @prj_cost[dsp_prj].key?(-1) # 値の無いプロジェクトはパス
 next if @prj_cost[dsp_prj][-1] == 0 # 値の無いプロジェクトはスパ
 prj =Project.find(dsp_prj)
%>
<tr style="background:#000;color:#fff;">
 <td>
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="JavaScript:prj_pos('<%=url_for(@link_params)%>', <%=dsp_prj%>,<%=dsp_pos%>,<%=prjs.size%>);">
      <%=dsp_pos%>
    </a>
<%end%>
   <%=prj.name%>
 </td>
 <td> <%=(@prj_cost[dsp_prj])[-1]%> </td>
<%
@members.each do |mem_info|
  user=mem_info[1]
  uid=user.id
%>
  <td>
    <%= @prj_cost[dsp_prj].key?(uid) ? (@prj_cost[dsp_prj])[uid] : "&nbsp;"%>
  </td>
<%
end
%>
</tr>
<%
tickets.each do |tic|
  issue_id = tic.issue_id
  next unless @issue_cost.key?(issue_id) # 値の無いチケットはパス
  next unless @issue_cost[issue_id].key?(-1) # 値の無いチケットはパス
  next if @issue_cost[issue_id][-1] == 0 # 値の無いチケットはパス

  issue = Issue.find_by_id(issue_id)
  next if issue.nil? # チケットが削除されていたらパス
  next if issue.project_id != dsp_prj # このプロジェクトに表示するチケットでない場合はパス

  issueHtml = issue.closed? ? "<del>"+issue.to_s+"</del>" : issue.to_s
%>
<tr>
<td>
<%if User.current.allowed_to?(:edit_work_time_total, @project) then%>
    <a href="JavaScript:ticket_pos('<%=url_for(@link_params)%>', <%=issue_id%>,<%=tic.position%>,<%=tic_max%>);">
      <%=tic.position%>
    </a>
<%end%>
  <%= link_to(issueHtml, {:controller=>"issues", :action=>"show", :id=>issue.id}, :popup=>true) %>
  <%= print_issue_cost(issue) %>
</td>

<td style="background:#ddd;">
<%=(@issue_cost[issue_id])[-1]%>
</td>

<%
@members.each do |mem_info|
  user=mem_info[1]
%>
<td>
<% if @issue_cost.key?(issue_id) && @issue_cost[issue_id].key?(user.id) then %>
<%= (@issue_cost[issue_id])[user.id] %>
<%else%>
&nbsp;
<%end%>
</td>
<%
end
%>

</tr>
<%
  end
end
if @prj_cost.has_key?(-1) then
%>
<tr style="background:#000;color:#fff;">
 <td>private</td>
 <td> <%=(@prj_cost[-1])[-1]%> </td>
<%
@members.each do |mem_info|
  user=mem_info[1]
  uid=user.id
%>
  <td>
    <%= @prj_cost[-1].key?(uid) ? (@prj_cost[-1])[uid] : "&nbsp;"%>
  </td>
<%end%>
</tr>
<%end%>
</table>

<div align="right">
<%= render :partial=>'select_project' %> <br/>
[<%= link_to(l(:wt_each_member_report), @link_params.merge(:action=>"show")) %>]
[<%= link_to(l(:wt_edit_relay), @link_params.merge(:action=>"edit_relay")) %>]
[<%= link_to(l(:wt_relay_total), @link_params.merge(:action=>"relay_total")) %>]
</div>
